package cn.maidouya.pet.controller;

import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.util.AjaxResult;
import cn.maidouya.basic.util.LoginContext;
import cn.maidouya.basic.util.PageList;
import cn.maidouya.pet.service.IPetService;
import cn.maidouya.pet.domain.Pet;
import cn.maidouya.pet.domain.PetDetail;
import cn.maidouya.pet.query.PetQuery;
import cn.maidouya.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(tags = "宠物领养接口",description = "宠物领养接口")
public class PetController {
    @Autowired
    private IPetService petService;
    /**
     *  根据宠物id查询详情页信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据宠物id查询详情页信息",notes = "根据宠物id查询详情页信息")
    public Pet loadById(@PathVariable Long id) {
        Pet one = petService.loadById(id);
        return one;
    }

    /**
     *   宠物和详情页回显
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "查找宠物",notes = "根据ID查找宠物")
    public PetDetail loadDetailByPetId(@PathVariable("id") Long id) {
        System.err.println(id);
        return petService.loadDetailByPetId(id);
    }
    @GetMapping
    @ApiOperation(value = "查找全部宠物",notes = "查找所有宠物信息")
    public List<Pet> loadAll() {
        List<Pet> list = petService.loadAll();
        return list;
    }
    @PostMapping
    @ApiOperation(value = "查找宠物分页数据",notes = "分页查找所有宠物信息")
    public PageList<Pet> queryData(@RequestBody PetQuery petQuery) {
        PageList<Pet> pageList = petService.queryData(petQuery);
        return pageList;
    }

    /**
     *   前端宠物首页展示
     * @param petQuery
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "前端首页查找分页和所有展示",notes = "前端首页查找分页和所有展示")
    public PageList<Pet> queryList(@RequestBody PetQuery petQuery) {
        petQuery.setState(1);
        PageList<Pet> pageList = petService.queryData(petQuery);
        return pageList;
    }
    @PutMapping
    @ApiOperation(value = "添加和修改宠物",notes = "添加和修改宠物、详情页")
    public AjaxResult saveAndUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() == null){
                petService.save(pet);
            } else {
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除宠物",notes = "根据id删除宠物和详情页")
    public AjaxResult delete(@PathVariable Long id) {
        System.err.println(id);
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     *   批量上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onOROffSale(ids,1); // 1上架 0下架
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }
    /**
     *   批量下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.onOROffSale(ids,0);// 1上架 0下架
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }
    /**
     *   批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            petService.batchDelete(ids); // 批量删除
            return AjaxResult.getResult();
        } catch (BusinessExcepiton e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("系统繁忙，稍后再试！");
        }
    }

    @PostMapping("/match")
    @ApiOperation(value = "右侧经典搭配查询",notes = "右侧经典搭配查询")
    public List<Pet> loadMatch() {
        return petService.loadMatch();
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "宠物和宠物详情页展示",notes = "宠物和宠物详情页展示")
    public Pet info(@PathVariable Long id) {
        return petService.loadByPetDetail(id);
    }

   // 领养宠物
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable Long id, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            petService.adopt(id,logininfo.getId());
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setMessage("领养失败" +e.getMessage());
        }
    }

}